package com.smarthome.web.property.controller.system;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.account.AccountService;
import com.smarthome.core.advicemanager.PropertyAdviceService;
import com.smarthome.domain.bean.ShAccount;
import com.smarthome.domain.bean.ShPropertyNotice;
import com.smarthome.domain.bean.ShUser;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.enums.AdviceStatusEnum;
import com.smarthome.domain.enums.PushMobileType;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.domain.vo.boss.TimeRange;
import com.smarthome.rpc.PushMobileMessageService;
import com.smarthome.web.property.utils.UserHolder;

/**
 * 
* @ClassName: PropertyAdviceController
* @Description: 物业公告管理
* @author cyj
* @date 2015年9月23日 下午3:19:36
*
 */
@Controller
@RequestMapping("/propertyAdviceController")
public class PropertyAdviceController {
	
	@Resource
    private PropertyAdviceService propertyAdviceService;
	
	@Resource
	private AccountService accountService;
	
	@Resource
	private PushMobileMessageService pushMobileMessageService;
	
	/**
	 * 
	* getPage(根据条件获取物业公告数据列表)
	* @Title: getPage
	* @Description: TODO
	* @param propertyNotice
	* @param helper
	* @param request
	* @return
	* DataGrid
	* @throws
	* @author cyj
	* @date 2015年9月23日 下午3:22:03
	 */
	@RequestMapping("/dataGrid")
    @ResponseBody
	public DataGrid getPage(ShPropertyNotice propertyNotice,PageHelper helper,TimeRange timeRange){
		if(timeRange.getStartRecordTime() != null) {
			propertyNotice.addQueryData("startRecordTime", timeRange.getStartRecordTime());
		}
		if(timeRange.getEndRecordTime() != null) {
			propertyNotice.addQueryData("endRecordTime", timeRange.getEndRecordTime());
		}
		if(timeRange.getStartPublishTime() != null) {
			propertyNotice.addQueryData("startPublishTime", timeRange.getStartPublishTime());
		}
		if(timeRange.getEndPublishTime() != null) {
			propertyNotice.addQueryData("endPublishTime", timeRange.getEndPublishTime());
		}
		
		Long userResidenceId =  UserHolder.getCurrentResidenceId();
		propertyNotice.setResidenceId(userResidenceId);
		
		return propertyAdviceService.qeuryDataGrid(propertyNotice, helper);
	}
	
	/**
	 * 
	* addPage(跳转新增页面)
	* @Title: addPage
	* @Description: TODO
	* @param request
	* @return
	* String
	* @throws
	* @author cyj
	* @date 2015年9月23日 下午3:25:58
	 */
	@RequestMapping("/addPage")
    public String addPage(HttpServletRequest request) {
        return "/system/adviceManager/propertyAdviceAdd";
	}
	
	
	/**
	 * 
	* add(新增物业公告)
	* @Title: add
	* @Description: TODO
	* @param propertyNotice
	* @return
	* Result
	* @throws
	* @author cyj
	* @date 2015年9月23日 下午3:26:36
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Result add(ShPropertyNotice propertyNotice) {
		if(StringUtils.isBlank(propertyNotice.getTitle())) {
			return Result.failure("请输入标题！");
		}
		if(propertyNotice.getStatus() == null) {
			return Result.failure("请选择状态！");
		}
		if(StringUtils.isBlank(propertyNotice.getContent())) {
			return Result.failure("请输入内容！");
		}
		Date date = new Date();
		try{
			//获取登录的系统用户
	    	ShUser shUser = UserHolder.getUser();
			propertyNotice.setRecordUserId(shUser.getId());
			propertyNotice.setGmtCreate(date);
			propertyNotice.setGmtModify(date);
			propertyNotice.setRecordTime(date);
			Long residenceId = UserHolder.getCurrentResidenceId();
			propertyNotice.setResidenceId(residenceId);
			if(AdviceStatusEnum.PUBLISHED.getCode() == propertyNotice.getStatus()) {
				propertyNotice.setPublishTime(date);
				ShAccount queryAccount = new ShAccount();
				queryAccount.setResidenceId(residenceId);
				List<String> accountIdsList = accountService.getShAccountIds(queryAccount);
				if(accountIdsList.size() > 0) {
					pushMobileMessageService.pushMsgToSome(propertyNotice.getTitle(), PushMobileType.PROPERTY.getCode(), accountIdsList.toArray(new String[accountIdsList.size()]));
				}
			}
			propertyAdviceService.add(propertyNotice);
		} catch(Exception e){
			e.printStackTrace();
			return Result.failure(e.getMessage());
			  
		}
		return Result.success("新增成功！");
	 }
	
	/**
	 * 
	* @Title: edit 
	* @Description: 修改物业公告 
	* @param propertyNotice
	* @return
	* Result
	* @throws 
	* @author cyj
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public Result edit(ShPropertyNotice propertyNotice) {
		if(StringUtils.isBlank(propertyNotice.getTitle())) {
			return Result.failure("请输入标题！");
		}
		if(propertyNotice.getStatus() == null) {
			return Result.failure("请选择状态！");
		}
		if(StringUtils.isBlank(propertyNotice.getContent())) {
			return Result.failure("请输入内容！");
		}
		Date date = new Date();
		try{
			ShPropertyNotice notice = propertyAdviceService.getById(propertyNotice.getId());

			BeanUtils.copyProperties(propertyNotice, notice);
			
			//获取登录的系统用户
	    	ShUser shUser = UserHolder.getUser();
	    	notice.setRecordUserId(shUser.getId());
	    	notice.setGmtModify(date);
	    	notice.setRecordTime(date);
	    	Long residenceId = UserHolder.getCurrentResidenceId();
			notice.setResidenceId(residenceId);
			if(AdviceStatusEnum.PUBLISHED.getCode() == notice.getStatus()) {
				notice.setPublishTime(date);
				ShAccount queryAccount = new ShAccount();
				queryAccount.setResidenceId(residenceId);
				List<String> accountIdsList = accountService.getShAccountIds(queryAccount);
				if(accountIdsList.size() > 0) {
					pushMobileMessageService.pushMsgToSome(notice.getTitle(), PushMobileType.PROPERTY.getCode(), accountIdsList.toArray(new String[accountIdsList.size()]));
				}
			}else {
				notice.setPublishTime(null);
			}
			
			
			propertyAdviceService.add(notice);
		} catch(Exception e){
			e.printStackTrace();
			return Result.failure(e.getMessage());
			  
		}
		return Result.success("修改成功！");
	 }
		
	  
	/**
	 * 
	* @Title: editPage 
	* @Description: 调整修改页面 
	* @param request
	* @param id
	* @return
	* String
	* @throws 
	* @author cyj
	 */
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request,Long id){
		request.setAttribute("propertyNotice", propertyAdviceService.getById(id));
		return "/system/adviceManager/propertyAdviceEdit";
	}
	
	
	  /**
	   * 
	  * publish(发布物业公告)
	  * @Title: publish
	  * @Description: TODO
	  * @param propertyNotice
	  * @return
	  * Result
	  * @throws
	  * @author cyj
	  * @date 2015年9月23日 下午8:46:39
	   */
	  /*@RequestMapping("/publish")
	  @ResponseBody
	  public Result publish(ShPropertyNotice propertyNotice) {
		  if(propertyNotice.getStatus().equals(AdviceStatusEnum.PUBLISHED.getCode())) {
			  return Result.failure("发布失败！该公告已经发布！");
		  }
		  try{
			  int count = propertyAdviceService.publish(propertyNotice);
			  if(count == 0) {
				  return Result.failure("发布失败！该公告已被删除！");
			  }
		  } catch(Exception e){
			  return Result.failure("发布失败！系统异常！");
		  }
		  return Result.success("发布成功！");
		  
	  }*/
	  
	  
	  /**
	   * 
	  * delete(删除物业公告)
	  * @Title: delete
	  * @Description: TODO
	  * @param id
	  * @return
	  * Result
	  * @throws
	  * @author cyj
	  * @date 2015年9月23日 下午3:27:29
	   */
	  @RequestMapping("/delete")
	  @ResponseBody
	  public Result delete(Long id) {
		  try{
			  propertyAdviceService.delete(id);
		  } catch(Exception e){
			  return Result.failure("公告被引用，不能删除");
		  }
		  return Result.success("删除成功！");
	  }
	  
	  /**
	   * 
	  * getComboData(获取物业公告所需的combo数据)
	  * @Title: getComboData
	  * @Description: TODO
	  * @return
	  * Map<String,Object>
	  * @throws
	  * @author cyj
	  * @date 2015年9月23日 下午3:27:52
	   */
	  @RequestMapping("/getComboData")
	  @ResponseBody
	  public Map<String, Object> getComboData() {
	  	  Map<String,Object> result = new HashMap<>();
    	  List<Combobox> status = propertyAdviceService.status();
    	  result.put("status", status);
    	  return result;
	  }
	
}
